#ifndef __MAININCLUDE_H_
#define __MAININCLUDE_H_

#include "sys.h"
#include "delay.h"
#include "stm32f4xx_gpio.h"
#include "Typedefs.h"

#ifdef  MAIN_DEF_SET
	#define  _MAIN_DEF_
#else
	#define  _MAIN_DEF_		extern 
#endif
	
#define J1939_MSG_LENGTH	4
#define J1939_DATA_LENGTH	4
#define J1939_RX_QUEUE_SIZE	8

#define RC_SUCCESS	1
#define RC_FAIL		0

#define UDS_OVERTIME   500     // 500ms

//PC
#define PC_USART               USART6
#define PC_DMA_BaseAddr        USART6_BASE
#define PC_GPIO_RX             GPIOG
#define PC_GPIO_TX             GPIOG
#define PC_GPIO_AF             GPIO_AF_USART6
#define PC_GPIO_PIN_RX         GPIO_Pin_9 
#define PC_GPIO_PIN_TX         GPIO_Pin_14
#define PC_GPIO_Source_RX      GPIO_PinSource9 
#define PC_GPIO_Source_TX      GPIO_PinSource14
#define PC_GPIO_RCC_Periph     RCC_AHB1Periph_GPIOG
#define PC_USART_RCC_Periph    RCC_APB2Periph_USART6

//CAN1
#define CAN1_GPIO              GPIOA
#define CAN1_GPIO_PIN_RX       GPIO_Pin_11 
#define CAN1_GPIO_PIN_TX       GPIO_Pin_12
#define CAN1_GPIO_Source_RX    GPIO_PinSource11 
#define CAN1_GPIO_Source_TX    GPIO_PinSource12
#define CAN1_GPIO_RCC_Periph   RCC_AHB1Periph_GPIOA
#define CAN1_GPIO_EN           GPIOC
#define CAN1_GPIO_PIN_EN       GPIO_Pin_9 
#define CAN1_GPIO_EN_RCC       RCC_AHB1Periph_GPIOC
#define CAN1_EN                GPIO_ResetBits(CAN1_GPIO_EN, CAN1_GPIO_PIN_EN)


union J1939_MESSAGE_UNION {
	struct {
		u16    EXTMSGID_L:16;  // 0:15
		u16    EXTMSGID_H:2;   // 16:17
		u16    STDMSGID:11;    // 18:28  
   		u16    AAM:1;          // 29   
   		u16    AME:1;          // 30
   		u16    IDE:1;          // 31   
		u16    DataLength:4;
		u16    RTR:1;
		u16    Res2:3;
		u16	   TransmitPriority:5;
		u16	   Res3:3;
		u16	   Reserved:16;
	 	u16    Data[J1939_DATA_LENGTH];
	}FormatMsg;
	unsigned long Array[J1939_MSG_LENGTH];
};
typedef union J1939_MESSAGE_UNION J1939_MESSAGE;

typedef struct {
	unsigned char RxSuccess;  //成功接收到报文
	unsigned char LoadStep;   //升级步骤	
	unsigned char RxSum;
	unsigned char RxData[99];
	unsigned char TxData[99];
	uint16        RxDataHalfWord[64];
	uint32        LoadResetCounter;  //升级失败复位
}SciLoadInfo;

typedef struct {
	unsigned char LoadStep;         // 1: 进入升级
	uint32        AppLen;
	uint32        ReciveLen;
	uint32        LoadPadeCounter;  //升级标志
	unsigned char RxSum;
	unsigned char RxData[99];
	uint16        RxDataHalfWord[64];
	uint32        LoadResetCounter;  //升级失败复位
}CanLoadInfo;

/*控制收发引脚*/
//进入接收模式,必须要有延时等待485处理完数据                                                       //new
#define RS485_RX_EN()			0//{GPIO_ResetBits(GPIOA,GPIO_Pin_1);}
//进入发送模式,必须要有延时等待485处理完数据
#define RS485_TX_EN()			0//{GPIO_SetBits(GPIOA,GPIO_Pin_1);}

_MAIN_DEF_ void SendOneMessage( J1939_MESSAGE *MsgPtr,Uint16 Id);

extern void PCUSART_IRQ0p1ms(void);
extern void UartUpLoadMain(void);
extern void ReadDevInfo(void);

#ifdef  MAIN_DEF_SET
_MAIN_DEF_ unsigned char				   RXHead=0;
_MAIN_DEF_ unsigned char				   RXTail=0;
_MAIN_DEF_ unsigned char				   RXQueueCount=0;
_MAIN_DEF_ J1939_MESSAGE				   RXQueue[J1939_RX_QUEUE_SIZE]={0};
_MAIN_DEF_ J1939_MESSAGE				   OneMessage={0};
_MAIN_DEF_ J1939_MESSAGE				   TxMessage={0};
_MAIN_DEF_ unsigned char				   RXBUF[8]={0};
_MAIN_DEF_ unsigned char				   TXBUF[8]={0};
_MAIN_DEF_ unsigned char				   uds_branchprogram=0;
_MAIN_DEF_ u32							   BusOffTime=0;
_MAIN_DEF_ u32							   BusOffResetTime=0;
#else
_MAIN_DEF_ unsigned char 				   RXHead;
_MAIN_DEF_ unsigned char 				   RXTail;
_MAIN_DEF_ unsigned char 				   RXQueueCount;
_MAIN_DEF_ J1939_MESSAGE 				   RXQueue[J1939_RX_QUEUE_SIZE];
_MAIN_DEF_ J1939_MESSAGE 		           OneMessage;
_MAIN_DEF_ J1939_MESSAGE 		           TxMessage;
_MAIN_DEF_ unsigned char                   RXBUF[8];
_MAIN_DEF_ unsigned char                   TXBUF[8];
_MAIN_DEF_ unsigned char                   uds_branchprogram;
_MAIN_DEF_ u32                             BusOffTime;
_MAIN_DEF_ u32                             BusOffResetTime;
#endif

#endif



